<html>
<head><meta charset="utf-8"><title>C-like shorthand for testing result of assignment · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html">C-like shorthand for testing result of assignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240508507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240508507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240508507">(May 27 2021 at 18:11)</a>:</h4>
<p>Coming from a background in C, I was hoping to be able to write something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">v</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</code></pre></div>
<p>or even this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">v</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</code></pre></div>
<p>But it looks like those don't work because of the following:</p>
<ul>
<li><code>ShrAssign</code> doesn't return the resulting value. It returns <code>()</code> instead.</li>
<li>Integer types are not implicitly coerced to bool (this is a fine compromise).</li>
</ul>
<p>Wanted to make sure I'm not missing out on any other nice ways to do write this in rust. This alternative is kinda clunky:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">v</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="240509358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240509358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240509358">(May 27 2021 at 18:18)</a>:</h4>
<p>well, you could</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="240509441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240509441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240509441">(May 27 2021 at 18:19)</a>:</h4>
<p>Ah, very nice. Thanks.</p>



<a name="240510259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240510259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240510259">(May 27 2021 at 18:25)</a>:</h4>
<p>You could also use an iterator to generate the values you want: <a href="https://doc.rust-lang.org/std/iter/fn.successors.html">https://doc.rust-lang.org/std/iter/fn.successors.html</a></p>



<a name="240510611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240510611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240510611">(May 27 2021 at 18:28)</a>:</h4>
<p>Something like <code>for v in successors(Some(v), |v| v &gt;&gt; 7).take_while(|n| n != 0)</code></p>



<a name="240510719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240510719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240510719">(May 27 2021 at 18:28)</a>:</h4>
<p>(that could be written many other ways, and in particular could be written with just successors and a condition inside the closure)</p>



<a name="240511558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C-like%20shorthand%20for%20testing%20result%20of%20assignment/near/240511558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C-like.20shorthand.20for.20testing.20result.20of.20assignment.html#240511558">(May 27 2021 at 18:36)</a>:</h4>
<p>Note that clippy is not happy about mutation only within the <code>while</code> condition. This has been fixed recently, but not available in the latest release yet. <a href="https://github.com/rust-lang/rust-clippy/issues/6689">https://github.com/rust-lang/rust-clippy/issues/6689</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>